"""
Case Type   : 系统内部使用工具
Case Name   : 指定--bindir参数打印用户可执行文件的路径是否成功
Create At   : 2020/9/16
Owner       : opentestcase003
Description :
    1.获取GAUSSHOME路径
    2.执行命令打印用户可执行文件信息
Expect      :
    1.获取GAUSSHOME路径成功
    2.执行命令打印用户可执行文件信息成功
History     :
    modified: by @lihongji 2025/1/21 优化GAUSSHOME路径获取方式
"""

import unittest
import os

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

LOG = Logger()


class SystemInternalTools(unittest.TestCase):
    def setUp(self):
        LOG.info('-------------------this is setup--------------------')
        LOG.info('---Opengauss_Function_Tools_pg_config_Case0001开始执行---')
        self.constant = Constant()
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.PrimaryNode = Node('PrimaryDbUser')

    def test_system_internal_tools(self):
        text = '----step1:获取GAUSSHOME路径  expect:路径获取成功-----'
        LOG.info(text)
        excute_cmd1 = f' cat {macro.DB_ENV_PATH} | grep GAUSSHOME='
        LOG.info(excute_cmd1)
        msg1 = self.PrimaryNode.sh(excute_cmd1).result()
        gaussHome_path = msg1.split('=')[-1]
        LOG.info(gaussHome_path)
        self.assertIn('app', gaussHome_path, '执行失败:' + text)
        
        text = '----step2:执行命令打印用户可执行文件信息  expect:成功------'
        LOG.info(text)
        bin_cmd = f'''source {macro.DB_ENV_PATH};
            pg_config --bindir;'''
        LOG.info(bin_cmd)
        bin_msg = self.PrimaryNode.sh(bin_cmd).result()
        LOG.info(bin_msg)
        bin_path = os.path.join(gaussHome_path, 'bin')
        LOG.info(bin_path)
        self.assertIn(bin_path, bin_msg)

    def tearDown(self):
        LOG.info('-----------------this is tearDown-------------------')
        # 无需清理环境
        LOG.info('---Opengauss_Function_Tools_pg_config_Case0001执行完成---')
